package Function.ItemUse;

import FCRendering.FCRBattleOrderConstant;
import FCRendering.FCRConstants;
import VWorldGameStudio.*;

import java.util.HashMap;
import java.util.function.BiConsumer;

import static FCRendering.FCRMaster.fcv;

//投掷的都是单体攻击
public class TrowTypeItemUse extends AItemUse {
    @Override
    public ItemUseResult directUse() {
        return new ItemUseResult().useless();
    }

    @Override
    public ItemUseResult useInMapTarget(int x, int y) {
        return new ItemUseResult().useless();
    }

    @Override
    public ItemUseResult useForMonsterTarget(FCBattleOrder battleOrder, FCMonster m) {
        FCPlayerBattleWrap wrap = FCPlayerBattleWrap.findBattleWrapByName(keepPlayer.getAliasUID(), keepPlayer.getName());
        int hitRate = keepPlayer.getHitRate(wrap);
        int cirt = keepPlayer.getCritRate(wrap);//暴击值（最大100）
        HashMap<String,String> msTips = new HashMap<>();
        if (battleOrder.randomV2 * 100 > m.getAttackRate(hitRate)) {
            //TODO 未命中
            fcv.getMessage().add((fcv.getBattleMonster().indexOf(m) + 1) +"号"+m.getChName()+" 被攻击未命中");
            fcv.showMessage();
            System.out.println((fcv.getBattleMonster().indexOf(m) + 1) +"号"+m.getChName()+" 被攻击未命中");

            fcv.endBattleInfo((fcv.getBattleMonster().indexOf(m) + 1) +"号"+m.getChName()+" 被攻击未命中");
            return null;
        }
        if (useItem.getDesdefense() > 0) {
            //TODO 有甲的就破甲，没的就pass
            if (m.getPlateArmour() > 0) {
                int plate = m.getPlateArmour() - useItem.getDesdefense();
                plate = Math.max(0, plate);
                m.setPlateArmour(plate);
//                if (m.getPlateArmour() == 0) {
//                    fcv.getMessage().add(FCRConstants.SYSTEM + FCRConstants.BATTLE + m.getChName() + "-" + (fcv.getBattleMonster().indexOf(m) + 1) + "号 被完全破甲了");
//                    fcv.showMessage();
//                }
            }
        }

        //STEP 1 判断破坏和破甲的
        if (useItem.getSubdefense() > 0) {
            MstAttack mstAttack = m.getMinDefenseMSAttack();
            if (mstAttack != null && mstAttack.getDefense() > 0) {
                int de = mstAttack.getDefense() - useItem.getSubdefense();
                de = Math.max(0, de);
                mstAttack.setDefense(de);
//                if (mstAttack.getDefense() == 0) {
//                    fcv.getMessage().add(FCRConstants.SYSTEM + FCRConstants.BATTLE + m.getChName() + "-" + (fcv.getBattleMonster().indexOf(m) + 1) + "号的" + mstAttack.getName() + "被破坏了");
//                    fcv.showMessage();
//                }
            }
        }

        //STEP 2 判断攻击的，暴击加成的当次有效
        if (useItem.getAttackHurt() > 0) {
            boolean isBaoJi = false;
            int attckHurt = (int) (useItem.getAttackHurt() + keepPlayer.getStrength() / 10);

            int addAttack = wrap.getLongTermVal(FCRBattleOrderConstant.S_AddAttack);
            if(addAttack != -1){
                attckHurt += addAttack;
            }

            if (battleOrder.randomV3 * 100 < (cirt + useItem.getCrit() * 100)) {//产生了暴击
                attckHurt += attckHurt;
                isBaoJi = true;
            }
            if (m.getPlateArmour() > 0) {//怪物还有甲，则伤害要减少了,掉3/4 attckHurt甲和1/4伤害再-防御
                int plate = m.getPlateArmour() - 3 * attckHurt / 4;
                plate = Math.max(0, plate);
                m.setPlateArmour(plate);
                attckHurt = (int) (attckHurt / 4);
            }
            attckHurt = attckHurt - m.getMsDefense();
            attckHurt = Math.max(attckHurt, 1);
            int msBlood = m.getBlood() - attckHurt;
            msBlood = Math.max(0, msBlood);
            m.setShowAttckHurt(attckHurt);
            m.setBlood(msBlood);
            fcv.getMessage().add((fcv.getBattleMonster().indexOf(m) + 1) +"号"+m.getChName()+" HP减"+attckHurt+(isBaoJi ? "(~会心一击)" : ""));
            fcv.showMessage();

            System.out.println((fcv.getBattleMonster().indexOf(m) + 1) +"号"+m.getChName()+" HP减"+attckHurt+(isBaoJi ? "(~会心一击)" : ""));


            fcv.endBattleInfo((fcv.getBattleMonster().indexOf(m) + 1) +"号"+m.getChName()+" HP减"+attckHurt+(isBaoJi ? "(~会心一击)" : ""));
        }

        if(!m.getIsDeath()) {
            //STEP 3 攻击属性的
            if (!useItem.getElements().equals("null")) {
                WeaponTypeItemUse.checkElement(msTips, useItem, m, useItem.getElements(), battleOrder.randomV4);
            }
            //STEP 4 速度
            if (useItem.getSpeedAdd() != 0) {
                m.addlongTermState(FCRBattleOrderConstant.S_AddSpeed, useItem.getSpeedAdd(), useItem.getAttackRound());
            }
        }

        msTips.forEach(new BiConsumer<String, String>() {
            @Override
            public void accept(String s, String s2) {
                fcv.getMessage().add(s+s2);
                fcv.showMessage();
                fcv.endBattleInfo(s+s2);
            }
        });

        if(isInTankBag){
            keepItemTank.insertItemToKnapsack(positionInBag,"null");
            keepItemTank.reInitShuxing();
        }else {
            keepPlayer.insertItemToKnapsack(positionInBag,"null");
        }
        return null;
    }

    @Override
    public ItemUseResult useForPlayerTarget(FCPlayerInfo p) {
        return null;
    }

    @Override
    public ItemUseResult useForTankTarget(FCTank t) {
        return null;
    }
}
